package com.test.demo.service.impl;

import com.test.demo.mapper.AnswerMapper;
import com.test.demo.mapper.QuestionMapper;
import com.test.demo.po.Answer;
import com.test.demo.po.Question;
import com.test.demo.service.AnswerService;
import com.test.demo.service.QAService;
import com.test.demo.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Cenoo on 2017/7/3.
 */

@Service
public class QAServiceImpl implements QAService{

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    AnswerMapper answerMapper;

    @Override
    public int addQuestion(Question question) throws Exception {
        return questionMapper.insert(question);
    }

    @Override
    public List<Question> searchQuestion(String keyword) throws Exception {
        return questionMapper.selectByKeyword(keyword);
    }

    @Override
    public List<Question> searchByTag(String keyword) throws Exception {
        return questionMapper.selectByTag(keyword);
    }

    @Override
    public List<Question> getAllQuestion() throws Exception {
        return questionMapper.selectAll();
    }

    @Override
    public int addReply(Integer questionId) throws Exception {
        return questionMapper.addReply(questionId);
    }

    @Override
    public List<Answer> getAnswersOfQuestion(int questionId) {
        return answerMapper.selectByQuestion(questionId);
    }

    @Transactional
    @Override
    public int addAnswer(Answer answer) throws Exception {
        int result = 0;
        result = answerMapper.insert(answer);
        result += questionMapper.addReply(answer.getQuestionId());
        return result;
    }

}
